package cn.clq.basic.controller;

import cn.clq.basic.dto.RegisterDto;
import cn.clq.basic.exception.BusinessException;
import cn.clq.basic.service.IVerifyCodeService;
import cn.clq.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: 生成验证码的接口
 * @date 2022/6/15 18:55
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;
    /**
     * @description:发送图形验证码
     * @param: uuid
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/16 20:45
     */
    @GetMapping("/image/{uuid}")
    public AjaxResult imageCode(@PathVariable("uuid") String uuid ){
        try {
            String base64Str = verifyCodeService.imageCode(uuid);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * @description:发送手机验证码
     * @param: dto
     * @return: cn.clq.basic.util.AjaxResult
     * @author clqlongbao
     * @date: 2022/6/16 20:53
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.sendSmsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
}
